<template>
  <div class="userForm">
    <!-- element组件库的Dialog对话框默认可以通过点击 modal 关闭 Dialog，即点击空白处弹框可关闭。 -->
    <!-- :visible.sync 子组键改变父组件的值 控制子组件的显示-->
    <el-dialog title="修改比赛记录信息" :visible.sync="editDialogIsShow" :before-close="handleClose" :close-on-click-modal="false">
      <el-form :model="competitionRecords" label-width="80px"  :rules="editUserRules" ref="editUserForm">
        <el-form-item label="开始时间" prop="crtime">
          <el-col :span="11">
            <el-date-picker  placeholder="选择日期" v-model="competitionRecords.crtime" style="width: 100%;"
                             type="datetime"
                             format="yyyy-MM-dd hh:mm"
                             value-format="yyyy-MM-dd hh:mm"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="主队编号" prop="htn">
          <el-col :span="16">
            <el-input v-model="competitionRecords.htn" placeholder="请填入主队编号" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="客队编号" prop="tno">
          <el-col :span="16">
            <el-input v-model="competitionRecords.tno" placeholder="请填写客队编号" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="直播管理员编号" prop="lbano">
          <el-col :span="16">
            <el-input v-model="competitionRecords.lbano" placeholder="请填写直播管理员编号" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="比赛记录编号" prop="rcno">
          <el-col :span="16">
            <el-input v-model="competitionRecords.rcno" placeholder="请填写比赛记录编号期" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="结束时间" prop="rcetime">
          <el-col :span="11">
            <el-date-picker  placeholder="选择结束时间" v-model="competitionRecords.rcetime" style="width: 100%;"
                             type="datetime"
                             format="yyyy-MM-dd hh:mm"
                             value-format="yyyy-MM-dd hh:mm"
            ></el-date-picker>
          </el-col>
        </el-form-item>
        <el-form-item label="比赛地点" prop="rcplace">
          <el-col :span="16">
            <el-input v-model="competitionRecords.rcplace" placeholder="请填写比赛地点" clearable/>
          </el-col>
        </el-form-item>
        <el-form-item label="比赛天气" prop="rcw">
          <el-col :span="16">
            <el-input v-model="competitionRecords.rcw" placeholder="请填写比赛天气" clearable/>
          </el-col>
        </el-form-item>
        <!--  <el-form-item label="状态">
            <el-col :span="3">
              <el-select v-model="user.ustate" placeholder="请选择">
                <el-option v-for="(usergroup,index) in allUserGroup" :key="usergroup.num" :label="usergroup.userName" :value="usergroup.userName"/>
              </el-select>
            </el-col>
          </el-form-item>-->
        <!-- <el-form-item label="邮件" prop="uemail">
           <el-col :span="16">
             <el-input v-model="user.uemail" placeholder="请填入邮件" @input="updateView($event)" clearable />
           </el-col>
         </el-form-item>
         <el-form-item label="电话" prop="uphone">
           <el-col :span="16">
             <el-input v-model="user.uphone" placeholder="请填入电话号码" @input="updateView($event)" clearable />
           </el-col>
         </el-form-item>
         <el-form-item label="籍贯">
           <el-cascader
             size="large"
             :options="options"
             v-model="uaddr"
             @change="handleChange"
           >
           </el-cascader>
         </el-form-item>-->
      </el-form>
      <el-row>
        <el-col :span="12">
          <span>&nbsp;</span></el-col>
        <el-col :span="6">
          <span><el-button type="primary" @click="editCompetitionRecords('editUserForm')">修改</el-button></span>
        </el-col>
      </el-row>
    </el-dialog>
  </div>
</template>

<script>
  import api from "../../../../axios/api"
  import rules from "../../../../static/js/validator/rules.js"
  import utils from "../../../../static/js/utils/upload.js"
  import { regionData, CodeToText } from "element-china-area-data";
  export default {
    name: "editUser",
    data(){
      return {
        //行政区使用
        options: regionData,
        uaddr: [],
        //行政区使用结束

        oldImg: false,
        //上传使用
        fileList: [[],[],[]], //缓存区文件
        uploadFile:[[],[],[]], //  上传用文件
        formData:{files:[]},
        imagesUp:null,
        //上传使用结束

        competitionRecords:{},
        checkAll: false,
        //checkedFavs: ['唱歌', '游泳'],
        isIndeterminate: true,
        allUserGroup:[{num:0,"userName":"在线"},{num:1,"userName":"离线"}],
        editUserRules:{name:[{required:true,message:"姓名不能为空",trigger:'blur'},{trigger:'blur',validator:this.validateName},
            {max:20,min:2,message:'长度异常 长度在2-20之间',trigger:'blur'},{pattern:/^[\u0391-\uFFE5A-Za-z]+$/,message:'姓名必须为中文',trigger:'blur'}],
          uemail:[{required:true,message:"邮件不能为空",trigger:'blur'},{trigger:'change',validator:rules.FormValidate.Form().validateEmail}]},
        editDialogIsShow:false

      }
    },
    methods: {
      /*        getImgUrl: function (uimg) {
                return "/api/upload/images/user/" + uimg;
              },*/


      validateName:function(rule,value,callback){
        console.log(rule);
        if(value==1) {
          callback(new Error("1不正常"));
        }else{
          callback();
        }
      },
      init(competitionRecords) {
        this.fileList = [[],[],[]] //缓存区文件
        this.uploadFile = [[],[],[]],
          this.editDialogIsShow = true;
        this.competitionRecords = competitionRecords;
        console.log(competitionRecords);
        /*if(this.user.uimg!=null&&this.user.uimg!=''){
          this.oldImg = true;
        }*/
      },
      //行政区使用
      handleChange() {
        var loc = "";
        var regCode = "";
        for (let i = 0; i < this.uaddr.length; i++) {
          loc += CodeToText[this.uaddr[i]];
          regCode =this.uaddr[i]
        }
        console.log(loc);
        console.log(regCode);
        this.user.uaddr = loc+","+regCode;
      },
      //行政区使用结束


      handleCheckAllChange(val) {
        this.user.favs = val ? initfavOption : [];
        this.isIndeterminate = false;
      },
      handleCheckedFavsChange(value) {
        let checkedCount = value.length;
        this.checkAll = checkedCount === this.favs.length;
        this.isIndeterminate = checkedCount > 0 && checkedCount < this.favs.length;
      },
      //关闭页面
      handleClose() {
        this.editDialogIsShow = false;
        //给父组件传参
        this.$emit("editDialogIsShow", this.editDialogIsShow);
      },
      editCompetitionRecords:function(editUserForm){
        this.$refs[editUserForm].validate(valid => {//此处refs对象和标签上的ref对应
          if (valid) {
            //如果通过验证 to do...
            console.log(this.user);
            this.updateUser();
            this.handleClose();
          } else {
            console.log('error submit!!')
            return false
          }
        });

      },
      updateUser:function(){
        //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
        api.setDataFromAxios("/liveAdmin/competitionRecord",this.competitionRecords,"patch")
          .then(res=>{
            // this.oldImg = false;
            console.log(res);
            this.$message.success("修改成功!");
            this.leagues = res.leagues});//.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
      },
      updateView(e) {
        this.$forceUpdate();
      },

      imghandleBeforeUpload(file,type,vm) {

        return utils.UPLOAD.Upload().handleBeforeUpload(file,type,vm);
      },
      imgHandleChange(file, fileList, type,vm,modelName,name) {
        if(vm.fileList[type].length!=0){
          alert("请先删除再提交")
        }
        return utils.UPLOAD.Upload().handleChange(file, fileList, type,vm,modelName,name);
      },
      imgHandleRemove(type,vm) {

        return utils.UPLOAD.Upload().handleRemove( type,vm);
      },
      uploadImg:function(modelName,name){
        //  使用formdata格式
        let formData = new FormData();
        formData.append("model",modelName);//图片上传子目录
        console.log(formData);
        console.log(this.formData.files[name]);
        if (this.formData.files[name]) {
          formData.append("file", this.formData.files[name]);
        }
        console.log("----------------------------");
        console.log(formData);
        //为什么要封装一下方法 1.每个vue组件有自己的一些参数处理，简化调用本来这个setDataFromAxios这个方法属于api 重新封装就属于当前页面
        /*   api.setDataFromAxios("/api/uploadImg",formData,"post",{headers: { "Content-Type": "multipart/form-data" }})
             .then(dto=>{
               this.oldImg = false;
               this.$message.info("上传图片成功!");
               if(dto.code==200){
                 this.user.uimg = dto.obj;
               }
             });*///.then方法 就是当服务器有返回的时候执行回调函数，对比jquery的ajax success:function（data）
      },
    }/*,
      computed:{
        getUploadUrl(){
          return "/api/uploadImg";
        }
      }*/,


    //监听
    watch: {
      editDialogIsShow(newValue, oldValue) {//配合:visible.sync监听变化
        this.editDialogIsShow = newValue;
      }
    }
  }
</script>
<style scoped lang="stylus">
  .userForm
    margin 0 auto
    width 1000px
    text-align left
</style>
